package com.fmi.timedealer.plan;

import java.util.Calendar;
import java.util.Date;

import com.fmi.timedealer.R;
import com.fmi.timedealer.R.id;
import com.fmi.timedealer.R.layout;
import com.fmi.timedealer.datamanager.IDataManager;
import com.fmi.timedealer.datamanager.SQLiteDataManager;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

public class ManagePlanActivity extends Activity {

	public static final String PLAN_BUNDLE_KEY = "PlanObject";
	
	private Plan currentPlan = null;
	private IDataManager dbManager = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.manage_plan);
		
		dbManager = new SQLiteDataManager("MyDB", this);
		
		Bundle extras = getIntent().getExtras();
		currentPlan = (Plan)extras.getSerializable(PLAN_BUNDLE_KEY);
		
		fillViewsData(currentPlan);
		
		Button btnSave = (Button)findViewById(R.id.btn_save_plan);
		btnSave.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				updateCurrentPlanValues();
				if (currentPlan.getId() == 0) {
					dbManager.insertPlan(currentPlan);
				}
				else {
					dbManager.updatePlan(currentPlan);
				}
				returnResult(Activity.RESULT_OK);
			}
		});
		
		Button btnCancel = (Button)findViewById(R.id.btn_cancel_plan);
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				returnResult(Activity.RESULT_CANCELED);
			}
		});
	}
	
	private void fillViewsData(Plan plan) {
	
		// Fill Name data
		TextView tvPlanName = (TextView)findViewById(R.id.text_plan_name);
		if (tvPlanName != null) {
			tvPlanName.setText(plan.getName());
		}
		
		// Fill Description data
		TextView tvPlanDescription = (TextView)findViewById(R.id.text_plan_description);
		if (tvPlanDescription != null) {
			tvPlanDescription.setText(plan.getDescription());
		}
		
		Calendar cal = Calendar.getInstance();
		
		// Fill Start Date data
		DatePicker dpStartDate = (DatePicker)findViewById(R.id.dp_plan_startDate);
		if (dpStartDate != null) {
			cal.setTime(plan.getStart());
			dpStartDate.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), null);
		}
		
		// Fill End Date data
		DatePicker dpEndDate = (DatePicker)findViewById(R.id.dp_plan_endDate);
		if (dpEndDate != null) {
			cal.setTime(plan.getEnd());
			dpEndDate.init(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DATE), null);
		}
	}
	
	private void updateCurrentPlanValues() {
		
		int year = 0;
		int month = 0;
		int day = 0;
		
		// Fill Name data
		TextView tvPlanName = (TextView)findViewById(R.id.text_plan_name);
		if (tvPlanName != null) {
			currentPlan.setName(tvPlanName.getText().toString());
		}
		
		// Fill Description data
		TextView tvPlanDescription = (TextView)findViewById(R.id.text_plan_description);
		if (tvPlanDescription != null) {
			currentPlan.setDescription(tvPlanDescription.getText().toString());
		}
		
		// Fill Start Date data
		DatePicker dpStartDate = (DatePicker)findViewById(R.id.dp_plan_startDate);
		if (dpStartDate != null) {
			year = dpStartDate.getYear();
			month = dpStartDate.getMonth();
			day = dpStartDate.getDayOfMonth();
			currentPlan.setStart(new Date(year - 1900, month, day));
		}
		
		// Fill End Date data
		DatePicker dpEndDate = (DatePicker)findViewById(R.id.dp_plan_endDate);
		if (dpEndDate != null) {
			year = dpEndDate.getYear();
			month = dpEndDate.getMonth();
			day = dpEndDate.getDayOfMonth();
			currentPlan.setEnd(new Date(year - 1900, month, day));
		}
	}
	
	private void returnResult(int resultCode) {

		if (resultCode == RESULT_OK) {
			getIntent().removeExtra(PLAN_BUNDLE_KEY);
			getIntent().putExtra(PLAN_BUNDLE_KEY, currentPlan);
		}
		this.setResult(resultCode, getIntent());
		this.finish();
	}
}
